home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ISO_SRC.ZIP / LIBRARY.ZIP / VIDEO.C < prev   
Encoding:
C/C++ Source or Header  |  1996-05-12  |  10.6 KB  |  580 lines

  1. /*
  2.     Video functions      May 12, 1996
  3.  
  4.     New functions for WATCOM and BORLAND compilers.
  5.  
  6.     just use:
  7.  
  8.   #define COMPILER_WATCOM
  9.  
  10.   or
  11.  
  12.   #define COMPILER_BORLAND
  13.  
  14.   Error checks if both directives used.
  15. */
  16.  
  17. /*  ERROR CHECK FOR DEFINES */
  18. #ifdef COMPILER_BORLAND
  19.   #ifdef COMPILER_WATCOM
  20.     #error MUST USE ONLY ONE COMPILER_ OPTION
  21.   #endif
  22. #else
  23.   #ifndef COMPILER_WATCOM
  24.     #error MUST USE COMPILER_ OPTION
  25.   #endif
  26. #endif
  27.  
  28. /*
  29.   DO BORLAND STUFF
  30. */
  31. #ifdef COMPILER_BORLAND
  32.  
  33. #ifndef __DOS_H
  34. #include <dos.h>
  35. #endif
  36.  
  37. #ifndef __STDIO_H
  38. #include <stdio.h>
  39. #endif
  40.  
  41. #ifndef __CONIO_H
  42. #include <conio.h>
  43. #endif
  44.  
  45. #ifndef __MEM_H
  46. #include <mem.h>
  47. #endif
  48.  
  49. // define our video screen memory region
  50. char *gfx_screen=MK_FP(0xa000,0);
  51.  
  52. // video setting and getting functions
  53. void setvmode(short short int video_mode);
  54. signed short int getmode(void);
  55.  
  56. // palette setting and capturing functions
  57. void palette_set(char *pal,signed short int num_colors,char border_color);
  58. void palette_get(char *pal);
  59. void set_single_color(char color_number,char r,char g,char b);
  60.  
  61. // palette fading in and out functions
  62. void palette_fadein(char *pal,signed short int speed);
  63. void palette_fadeout(char *pal,signed short int speed);
  64.  
  65. // set current palette colors to black or white
  66. void black_pal(void);
  67. void white_pal(void);
  68.  
  69. // wait for vertical retrace
  70. void wait_vbi(void);
  71.  
  72. // set or get a graphics pixel
  73. void pixel(signed short int x,signed short int y,char color,char *screen);
  74. signed short int get_pixel(signed short int x,signed short int y,char *screen);
  75.  
  76. // blitting buffers, clearing buffers, copying buffers functions
  77. void display_screen(char *screen);
  78. void clear_screen(char *screen,char color);
  79. void copy_screen(char *dest,char *src);
  80.  
  81.  
  82. void setvmode(short short int video_mode)
  83. {
  84.   asm {
  85.     mov ax,video_mode
  86.     int 10h
  87.   }
  88. }
  89.  
  90. signed short int getmode(void)
  91. {
  92.   union REGS reg;
  93.  
  94.   reg.h.ah=0x0f;
  95.   int86(0x10,®,®);
  96.   return(reg.h.al);
  97. }
  98.  
  99. void palette_set(char *pal,signed short int num_colors,char border_color)
  100. {
  101.   signed short int i;
  102.  
  103.   // set all colors
  104.   outp(0x3c6,0xff);
  105.   for(i=0;i<num_colors;++i) {
  106.     outp(0x3c8,i);
  107.     outp(0x3c9,(*pal++) >> 2);
  108.     outp(0x3c9,(*pal++) >> 2);
  109.     outp(0x3c9,(*pal++) >> 2);
  110.   }
  111.  
  112.   // set border color
  113.   asm {
  114.     mov ax,0x1001
  115.     mov bh,border_color;
  116.     int 10h
  117.   }
  118. }
  119.  
  120. void palette_get(char *pal)
  121. {
  122.   char i;
  123.   char _red,_green,_blue;
  124.  
  125.   for(i=0;i<256;i++) {
  126.     asm {
  127.       mov dx,0x3c7
  128.       mov al,i
  129.       out dx,al
  130.  
  131.       add dx,2
  132.  
  133.       in     al,dx
  134.       mov    _red,al
  135.       in     al,dx
  136.       mov    _green,al
  137.       in     al,dx
  138.       mov    _blue,al
  139.     }
  140.     *pal++ = _red;
  141.     *pal++ = _green;
  142.     *pal++ = _blue;
  143.   }
  144. }
  145.  
  146. void set_single_color(char color_number,char r,char g,char b)
  147. {
  148.   asm {
  149.     mov    dx,0x3c8
  150.     mov    al,color_number
  151.     out    dx,al
  152.     inc    dx
  153.     mov    al,r
  154.     out    dx,al
  155.     mov    al,g
  156.     out    dx,al
  157.     mov    al,b
  158.     out    dx,al
  159.   }
  160. }
  161.  
  162. void palette_fadein(char *pal,signed short int speed)
  163. {
  164.   char pal2[768];
  165.   signed short int i,j;
  166.   signed short int max;
  167.  
  168.   max=256/speed;
  169.   memset(pal2,0,768);
  170.   for(i=0;i<max;i++) {
  171.     for(j=0;j<768;j++) {
  172.       if(pal2[j]+speed<pal[j])
  173.         pal2[j]+=speed;
  174.       else pal2[j]=pal[j];
  175.     }
  176.     palette_set(pal2,256,0);
  177.   }
  178.   palette_set(pal,256,0);
  179. }
  180.  
  181. void palette_fadeout(char *pal,signed short int speed)
  182. {
  183.   char pal2[768];
  184.   signed short int i,j;
  185.   signed short int max;
  186.  
  187.   max=256/speed;
  188.   memcpy(pal2,pal,768);
  189.   for(i=0;i<max;i++) {
  190.     for(j=0;j<768;j++) {
  191.       if(pal2[j]>speed)
  192.         pal2[j]-=speed;
  193.       else pal2[j]=0;
  194.     }
  195.     palette_set(pal2,256,0);
  196.   }
  197. }
  198.  
  199. void black_pal(void)
  200. {
  201.   char pal[768];
  202.  
  203.   memset(pal,0,768);
  204.   palette_set(pal,256,0);
  205. }
  206.  
  207. void white_pal(void)
  208. {
  209.   char pal[768];
  210.  
  211.   memset(pal,255,768);
  212.   palette_set(pal,256,0);
  213. }
  214.  
  215. // Wait for vertical retrace
  216. void wait_vbi(void)
  217. {
  218.   while((inp(0x3da)&8) !=0);
  219.   while((inp(0x3da)&8) ==0);
  220. /*
  221.   asm mov     dx,03DAh
  222.   vbi_1:
  223.   asm {
  224.     in    al,dx
  225.     test  al,08h
  226.     jnz   vbi_1
  227.   }
  228.   vbi_2:
  229.   asm {
  230.     in    al,dx
  231.     test  al,08h
  232.     jz    vbi_2
  233.   }
  234. */
  235. }
  236.  
  237. void pixel(signed short int x,signed short int y,char color,char *screen)
  238. {
  239.   if((unsigned short int)x<320 && (unsigned short int)y<200)
  240.     screen[(y<<8)+(y<<6)+x]=color;
  241. }
  242.  
  243. signed short int get_pixel(signed short int x,signed short int y,char *screen)
  244. {
  245.   if((unsigned short int)x<320 && (unsigned short int)y<200)
  246.     return(screen[(y<<6)+(y<<8)+x]);
  247.   return(-1);
  248. }
  249.  
  250. void display_screen(char *screen)
  251. {
  252.   char *gscreen=MK_FP(0xa000,0);
  253.  
  254.   asm {
  255.     push ds
  256.     push es
  257.  
  258.     lds si,screen
  259.     les di,gscreen
  260.     cld
  261.     mov cx,64000/2
  262.     rep movsw
  263.  
  264.     pop es
  265.     pop ds
  266.   }
  267. }
  268.  
  269. void clear_screen(char *screen,char color)
  270. {
  271.   asm {
  272.     push es
  273.     push di
  274.     mov al,color
  275.     mov ah,al
  276.     les di,screen
  277.     cld
  278.     mov cx,32000
  279.     rep stosw
  280.     pop di
  281.     pop es
  282.   }
  283. }
  284.  
  285. void copy_screen(char *dest,char *src)
  286. {
  287.   asm {
  288.     push ds
  289.     push es
  290.     push di
  291.     push si
  292.  
  293.     lds si,src
  294.     les di,dest
  295.     cld
  296.     mov cx,64000/2
  297.     rep movsw
  298.  
  299.     pop si
  300.     pop di
  301.     pop es
  302.     pop ds
  303.   }
  304. }
  305.  
  306. /*
  307. setmodex(char height)
  308. {
  309.   asm {
  310.     cld
  311.     mov ax,13h
  312.     int 10h
  313.     cli
  314.     mov dx,3c4h
  315.     mov AX,604h                    // Unchain VGA
  316.     out dx,ax
  317.     mov ax,0F02h                   // All planes...
  318.     out dx,ax
  319.  
  320.     mov dx,3D4h
  321.     mov ax,14h                     // Disable dword mode
  322.     out dx,ax
  323.     mov ax,0E317h                  // Enable byte mode.
  324.     out dx,ax
  325.     mov al,9
  326.     out dx,al
  327.     inc dx
  328.     in  al,dx
  329.     and al,0E0h
  330.     add al,height                  // Set height of scan
  331.     out dx,al
  332.   }
  333. }
  334.  
  335. {──────────────────────────────────────────────────────────────────────────}
  336. Procedure GetPal(Col : Byte; Var R,G,B : Byte);
  337.   { This gets the Red, Green and Blue values of a certain color }
  338. Var
  339.    rr,gg,bb : Byte;
  340. Begin
  341.    asm
  342.       mov    dx,3c7h
  343.       mov    al,col
  344.       out    dx,al
  345.  
  346.       add    dx,2
  347.  
  348.       in     al,dx
  349.       mov    [rr],al
  350.       in     al,dx
  351.       mov    [gg],al
  352.       in     al,dx
  353.       mov    [bb],al
  354.    end;
  355.    r := rr;
  356.    g := gg;
  357.    b := bb;
  358. end;
  359.  
  360. {──────────────────────────────────────────────────────────────────────────}
  361. procedure WaitRetrace; assembler;
  362.   {  This waits for a vertical retrace to reduce snow on the screen }
  363. label
  364.   l1, l2;
  365. asm
  366.     mov dx,3DAh
  367. l1:
  368.     in al,dx
  369.     and al,08h
  370.     jnz l1
  371. l2:
  372.     in al,dx
  373.     and al,08h
  374.     jz  l2
  375. end;
  376. */
  377.  
  378. #endif
  379.  
  380. /*
  381.   DO WATCOM STUFF
  382. */
  383. #ifdef COMPILER_WATCOM
  384.  
  385. #ifndef __DOS_H
  386. #include <dos.h>
  387. #endif
  388.  
  389. #ifndef __STDIO_H
  390. #include <stdio.h>
  391. #endif
  392.  
  393. #ifndef __CONIO_H
  394. #include <conio.h>
  395. #endif
  396.  
  397. #ifndef __MEM_H
  398. #include <mem.h>
  399. #endif
  400.  
  401. // define our video screen memory region
  402. char *gfx_screen=(char*)0xa0000;
  403.  
  404. // video setting and getting functions
  405. void setvmode(signed short int video_mode);
  406.  
  407. signed short int getmode(void);
  408.  
  409. // palette setting and capturing functions
  410. void palette_set(char *pal,signed short int num_colors,char border_color);
  411. void palette_get(char *pal);
  412. void set_single_color(char color_number,char r,char g,char b);
  413.  
  414. // palette fading in and out functions
  415. void palette_fadein(char *pal,signed short int speed);
  416. void palette_fadeout(char *pal,signed short int speed);
  417.  
  418. // set current palette colors to black or white
  419. void black_pal(void);
  420. void white_pal(void);
  421.  
  422. // wait for vertical retrace
  423. void wait_vbi(void);
  424.  
  425. // set or get a graphics pixel
  426. void pixel(signed short int x,signed short int y,char color,char *screen);
  427. signed short int get_pixel(signed short int x,signed short int y,char *screen);
  428.  
  429. // blitting buffers, clearing buffers, copying buffers functions
  430. void display_screen(char *screen);
  431. void clear_screen(char *screen,char color);
  432. void copy_screen(char *dest,char *src);
  433.  
  434. // setvmode
  435. #pragma aux setvmode =   \
  436.         "int 10h"               \
  437.     parm caller [eax];
  438. //    modify [eax];
  439.  
  440. signed short int getmode(void)
  441. {
  442.   union REGS reg;
  443.  
  444.   reg.h.ah=0x0f;
  445.   int386(0x10,®,®);
  446.   return(reg.h.al);
  447. }
  448.  
  449. void palette_set(char *pal,signed short int num_colors,char border_color)
  450. {
  451.   union REGS reg;
  452.   signed short int i;
  453.  
  454.   // set all colors
  455.   outp(0x3c6,0xff); //??
  456.   for(i=0;i<num_colors;++i) {
  457.     outp(0x3c8,i);
  458.     outp(0x3c9,(*pal++) >> 2);
  459.     outp(0x3c9,(*pal++) >> 2);
  460.     outp(0x3c9,(*pal++) >> 2);
  461.   }
  462.  
  463.   // set border color
  464.   reg.x.eax = 0x1001;
  465.   reg.h.bh = border_color;
  466.   int386(0x10,®,®);
  467. }
  468.  
  469.  
  470. void palette_get(char *pal)
  471. {
  472.   char i;
  473.  
  474.   for(i=0;i<256;i++) {
  475.     outp(0x3c7,i);
  476.     *pal++ = inp(0x3c9);
  477.     *pal++ = inp(0x3c9);
  478.     *pal++ = inp(0x3c9);
  479.   }
  480. }
  481.  
  482. void set_single_color(char color_number,char r,char g,char b)
  483. {
  484.   outp(0x3c8,color_number);
  485.   outp(0x3c9,r);
  486.   outp(0x3c9,g);
  487.   outp(0x3c9,b);
  488. }
  489.  
  490. void palette_fadein(char *pal,signed short int speed)
  491. {
  492.   char pal2[768];
  493.   signed short int i,j;
  494.   signed short int max;
  495.  
  496.   max=256/speed;
  497.   memset(pal2,0,768);
  498.   for(i=0;i<max;i++) {
  499.     for(j=0;j<768;j++) {
  500.       if(pal2[j]+speed<pal[j])
  501.         pal2[j]+=speed;
  502.       else pal2[j]=pal[j];
  503.     }
  504.     palette_set(pal2,256,0);
  505.   }
  506.   palette_set(pal,256,0);
  507. }
  508.  
  509. void palette_fadeout(char *pal,signed short int speed)
  510. {
  511.   char pal2[768];
  512.   signed short int i,j;
  513.   signed short int max;
  514.  
  515.   max=256/speed;
  516.   memcpy(pal2,pal,768);
  517.   for(i=0;i<max;i++) {
  518.     for(j=0;j<768;j++) {
  519.       if(pal2[j]>speed)
  520.         pal2[j]-=speed;
  521.       else pal2[j]=0;
  522.     }
  523.     palette_set(pal2,256,0);
  524.   }
  525. }
  526.  
  527. void black_pal(void)
  528. {
  529.   char pal[768];
  530.  
  531.   memset(pal,0,768);
  532.   palette_set(pal,256,0);
  533. }
  534.  
  535. void white_pal(void)
  536. {
  537.   char pal[768];
  538.  
  539.   memset(pal,255,768);
  540.   palette_set(pal,256,0);
  541. }
  542.  
  543. // Wait for vertical retrace
  544. void wait_vbi(void)
  545. {
  546.   while((inp(0x3da)&8) !=0);
  547.   while((inp(0x3da)&8) ==0);
  548. }
  549.  
  550. void pixel(signed short int x,signed short int y,char color,char *screen)
  551. {
  552.   if((unsigned short int)x<320 && (unsigned short int)y<200)
  553.     screen[(y<<8)+(y<<6)+x]=color;
  554. }
  555.  
  556. signed short int get_pixel(signed short int x,signed short int y,char *screen)
  557. {
  558.   if((unsigned short int)x<320 && (unsigned short int)y<200)
  559.     return(screen[(y<<6)+(y<<8)+x]);
  560.   return(-1);
  561. }
  562.  
  563. void display_screen(char *screen)
  564. {
  565.   memcpy(gfx_screen,screen,64000);
  566. }
  567.  
  568. void clear_screen(char *screen,char color)
  569. {
  570.   memset(screen,color,64000);
  571. }
  572.  
  573. void copy_screen(char *dest,char *src)
  574. {
  575.   memcpy(dest,src,64000);
  576. }
  577.  
  578. #endif
  579.  
  580.